/*
 * Created by JFormDesigner on Sun Jun 30 22:21:17 CST 2024
 */

package scct.hys.ui;

import java.awt.event.*;
import scct.hys.dao.MovieDao;
import scct.hys.dao.UserDao;
import scct.hys.model.Movie;
import scct.hys.model.User;

import java.awt.*;
import javax.net.ssl.SSLContext;
import javax.swing.*;

/**
 * @author 26984
 */
public class DlgMovieUpdate extends JDialog {
    int id;
    public DlgMovieUpdate(Window owner,int id) {
        super(owner);
        initComponents();
        this.id=id;
        initView();

    }

    private void initView() {
        MovieDao movieDao=new MovieDao();
        Movie movie=movieDao.getMovieById(id);
        txtMovieName.setText(movie.getName());
        txtDirectorName.setText(movie.getDirector());
        txtActor.setText(movie.getActors());
        txtReleasedate.setText(movie.getReleaseDate());
        txtDuration.setText(movie.getDuration());

    }

    private void btnUpdate(ActionEvent e) {

        // TODO add your code here
        String movieid=txtMovieName.getText();
        String director=txtDirectorName.getText();
        String actor=txtActor.getText();
        String releasedate=txtReleasedate.getText();
        String duration=txtDuration.getText();
        MovieDao movieDao=new MovieDao();
        if (movieDao.updateMovie(id,movieid,director,actor,releasedate,duration)){
            JOptionPane.showMessageDialog(this,"修改成功");
            dispose();
        }

    }





    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        label1 = new JLabel();
        txtMovieName = new JTextField();
        label2 = new JLabel();
        txtDirectorName = new JTextField();
        label3 = new JLabel();
        txtActor = new JTextField();
        label4 = new JLabel();
        label5 = new JLabel();
        txtReleasedate = new JTextField();
        txtDuration = new JTextField();
        btnUpdate = new JButton();
        btnCancel = new JButton();

        //======== this ========
        setTitle("\u4fee\u6539\u5f71\u7247");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u5f71\u7247\u540d\u79f0");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(30, 30), label1.getPreferredSize()));
        contentPane.add(txtMovieName);
        txtMovieName.setBounds(90, 23, 80, txtMovieName.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u5bfc\u6f14\u59d3\u540d");
        contentPane.add(label2);
        label2.setBounds(30, 70, label2.getPreferredSize().width, 17);
        contentPane.add(txtDirectorName);
        txtDirectorName.setBounds(90, 63, 127, txtDirectorName.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u4e3b\u6f14\u59d3\u540d");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(220, 70), label3.getPreferredSize()));
        contentPane.add(txtActor);
        txtActor.setBounds(275, 63, 110, txtActor.getPreferredSize().height);

        //---- label4 ----
        label4.setText("\u4e0a\u6620\u65e5\u671f");
        contentPane.add(label4);
        label4.setBounds(30, 106, label4.getPreferredSize().width, 17);

        //---- label5 ----
        label5.setText("\u7535\u5f71\u65f6\u957f");
        contentPane.add(label5);
        label5.setBounds(30, 144, label5.getPreferredSize().width, 17);
        contentPane.add(txtReleasedate);
        txtReleasedate.setBounds(90, 99, 127, 30);
        contentPane.add(txtDuration);
        txtDuration.setBounds(90, 137, 127, 30);

        //---- btnUpdate ----
        btnUpdate.setText("\u6dfb\u52a0");
        btnUpdate.addActionListener(e -> btnUpdate(e));
        contentPane.add(btnUpdate);
        btnUpdate.setBounds(new Rectangle(new Point(65, 205), btnUpdate.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        contentPane.add(btnCancel);
        btnCancel.setBounds(new Rectangle(new Point(235, 205), btnCancel.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JLabel label1;
    private JTextField txtMovieName;
    private JLabel label2;
    private JTextField txtDirectorName;
    private JLabel label3;
    private JTextField txtActor;
    private JLabel label4;
    private JLabel label5;
    private JTextField txtReleasedate;
    private JTextField txtDuration;
    private JButton btnUpdate;
    private JButton btnCancel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
